package pers.zh.parking.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import pers.zh.parking.common.BaseController;
import pers.zh.parking.common.Response;
import pers.zh.parking.po.Notice;
import pers.zh.parking.service.INoticeService;

import java.util.List;

@RestController
@RequestMapping("notice")
public class NoticeController extends BaseController {

    @Autowired
    INoticeService noticeService;

    @GetMapping("list")
    public Response list() {
        return Response.ok(noticeService.getAll());
    }

    @PostMapping("add")
    public Response add(@RequestBody Notice notice) {
        if (StringUtils.isEmpty(notice.getPublishUserId())) {
            return Response.fail_500("发布人不能为空");
        }
        if (StringUtils.isEmpty(notice.getNoticeTitle())) {
            return Response.fail_500("标题不能为空");
        }
        if (StringUtils.isEmpty(notice.getNoticeContent())) {
            return Response.fail_500("内容不能为空");
        }
        if (noticeService.add(notice)) {
            return Response.ok("通知下发成功");
        } else {
            return Response.fail_500("通知下发失败");
        }
    }

    @PostMapping("update")
    public Response update(@RequestBody Notice notice) {
        if (StringUtils.isEmpty(notice.getNoticeId())) {
            return Response.fail_500("id不能为空");
        }
        if (noticeService.updte(notice)) {
            return Response.ok("通知修改成功");
        } else {
            return Response.fail_500("通知修改失败");
        }
    }

    @PostMapping("delete")
    public Response delete(@RequestBody List<Notice> list) {
        if (ObjectUtils.isEmpty(list)) {
            return Response.fail_500("要删除的数据不能为空");
        }
        if (noticeService.delete(list)) {
            return Response.ok("通知删除成功");
        } else {
            return Response.fail_500("通知删除失败");
        }
    }
}
